<?php
namespace app\front\service\v1;
use app\front\model\Notify  as NotifyModel;
use app\front\model\Order as OrderModel;
use app\front\service\v1\RedPack as RedPackService;
class Order extends RedPackService
{
    private static $_instance;
    
    //private 构造函数
    private function __construct() {
       // echo " I'm construct! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    private function __clone() {
    //    echo " I'm clone! process id is " . getmypid() . " and thread id is " . Thread::getCurrentThreadId() . "\n";
    }
    
    //单例方法访问类实例
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }  
    
    /**
     * 函数用途描述 : 商户老板点击详情显示今天订单以及营业额
     * @date: 2018年2月5日 下午1:08:24
     * @author: 孙洪亮
     * @param: $store_id（店铺名）
     * @return:
     */
    public function showTodayOrder(){
        $store_id= input('param.store_id');;
        $starttime = strtotime(date("Y-m-d 00:00:00"));
        $starttime= date("Y-m-d H:i:s",$starttime);
        $endtime = strtotime(date("Y-m-d 23:59:59"));
        $endtime= date("Y-m-d H:i:s",$endtime);
        $res= OrderModel::where("create_time >='$starttime' and create_time <='$endtime'and store_id=$store_id")->select();  
      //  $res= json_decode(json_encode($res),true);
        return $res;
    }
    
    public function test($out_trade_no){
        $this->HongBao($out_trade_no);
    }
    public function insertOrder(){
      
        $limit=1;//每次执行数
        $orders = NotifyModel::where('status=0')->order('create_time')->limit($limit)
        ->field('oid,total_fee,out_trade_no,return_code,result_code,sign,status')->select();
        if($orders==null){
            $limit=0;
        }
        for($i=0;$i<$limit;$i++)
        {
            //将$user赋值为当前指针
            $order=current($orders); //dump($order);
            $order= json_decode(json_encode($order),true);
            $order['status']=1; print_r($order);// 
            NotifyModel::update($order);
            $out_trade_no=$order['out_trade_no'];
            $TemporaryMsg=$this->getTemporaryForMchId($out_trade_no);
            $customer_id=$TemporaryMsg['open_id'];
            $store_id =$TemporaryMsg['store_id'];
            $moneyNum = $TemporaryMsg['total_fee'];       
            $moneyNum= bcdiv($moneyNum,100,2);
            $statu=1;  
            if($order['return_code'] == 'SUCCESS' && $order['result_code'] == 'SUCCESS'){
                //生产环境需要根据订单号来查询价格
                if(bccomp($order['total_fee'],$moneyNum*100)==0){
                    $this->order_add($out_trade_no,$customer_id, $store_id, $moneyNum,$statu,$desc='交易成功');
                    $this->HongBao($out_trade_no);//$this->HongBao($out_trade_no);
                    
                }else{
                    $this->order_add($out_trade_no,$customer_id, $store_id, $moneyNum,$statu,$desc='金额有误');
                }
            }else{
                $this->order_add($out_trade_no,$customer_id, $store_id, $moneyNum,$statu,$desc='业务结果不正确');
            }
            
            
            $order=next($orders);
        }
        
    }
}


